import { API, rootPath, wsPath } from "../../utils/const";
import { CreateHeader } from "../../utils/request";
import webSocketManager from '../../utils/webSocketManager';

// pages/doctorview/doctorview.js
Page({
  data: {
    lowRiskList: [],
    mediumRiskList: [],
    highRiskList: [],
    lowRiskVisible: true,
    mediumRiskVisible: true,
    highRiskVisible: true
  },

  onLoad: function () {
    this.fetchData();
  },
  onUnload() {
  },
  
  handleLowRiskUpdate(data) {
    this.setData({
      lowRiskList: data
    });
  },
  
  fetchData: function () {
    const that = this;
    let header = CreateHeader(API.login, 'POST');
    let userInfo = wx.getStorageSync('userInfo');
    console.log('Calling...: ' + rootPath + API.riskPatient);
    console.log(userInfo)
    wx.request({
      url: rootPath + API.riskPatient, // Replace with your actual API endpoint
      header: header,
      data: {'doctorId': userInfo.userId},
      method: 'GET',
      timeout: 2000,
      success(res) {
        if (res.statusCode === 200) {
          console.log(res.data);
          let response = res.data;
          let lowRiskList = [];
          let mediumRiskList = [];
          let highRiskList = [];
          response.forEach(element => {
              console.log(element.riskLevel)
              if (element.riskLevel === 1){
                  lowRiskList.push(element);
              }
              else if(element.riskLevel === 2){
                  mediumRiskList.push(element);
              }
              else if(element.riskLevel === 3){
                  highRiskList.push(element);
              }
          });
          that.setData({
              lowRiskList: lowRiskList,
              mediumRiskList: mediumRiskList,
              highRiskList: highRiskList
            });
        } else {
          console.error('Failed to fetch data, use local');
        }
      },
      fail(err) {
        console.error('Request failed', err);
      }
    });    
  },

  toggleSection: function (e) {
    const section = e.currentTarget.dataset.section;
    this.setData({
      [`${section}Visible`]: !this.data[`${section}Visible`]
    });
  }
});
  